<template>
<van-icon :class="{TextColor: value===1}" :name="value===1 ? 'good-job' : 'good-job-o'" :loading="isbtnLoading" @click="like" />
</template>

<script>
import { likeArticle, cancelLike } from '@/api/user'
export default {
  name: 'likeIndex',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    artId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      isbtnLoading: false
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {

  },
  methods: {
    async like() {
      this.isbtnLoading = true
      let status = -1
      try {
        if (this.value === 1) {
          // 已点赞(要取消)
          await cancelLike(this.artId)
          status = -1
          this.$toast.success('取消点赞成功')
        } else {
          // 未点赞(要点赞)
          await likeArticle(this.artId)
          status = 1
          this.$toast.success('点赞成功')
        }
        this.$emit('input', status)
      } catch (err) {
        this.$toast.fail('操作失败，请重试')
      }
      this.isbtnLoading = false
    }
  }
}
</script>

<style scoped lang="less">
.TextColor {
    color:red;
}
</style>
